package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.location.LocationDTO;
import io.github.talelin.latticy.dto.location.UpdateLocationDTO;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.LocationDO;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * 仓库前端控制器
 *
 * @author generator@wulongbo
 * @since 2022-06-07
 */
@RestController
@RequestMapping("/v1/location")
@Validated
@PermissionModule(value = "库位管理")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "创建库位")
    @Logger(template = "{user.username}创建库位:{request.name}")
    public CreatedVO create(@RequestBody @Validated LocationDTO validator) {
        locationService.createLocation(validator);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "编辑库位")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated UpdateLocationDTO dto) {
        locationService.updateLocation(id, dto);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除库位")
    @Logger(template = "{user.username}删除库位:{request.locationMapper-name}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        locationService.deleteLocation(id);
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public LocationDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return locationService.getLocationById(id);
    }

    @GetMapping("/page/{areaId}")
    @GroupRequired
    @PermissionMeta(value = "查询库位")
    public PageResponseVO<LocationDO> page(@PathVariable(value = "areaId") @Positive(message = "{id.positive}") Integer areaId, @Validated BasePageDTO dto) {
        IPage<LocationDO> iPage = locationService.getLocationPage(dto.getPage(), dto.getCount(), areaId);
        return PageUtil.build(iPage);
    }


    @GetMapping("/getLocationList")
    @GroupRequired
    @PermissionMeta(value = "查询库位")
    public List<LocationDO> getLocationList(LocationDO locationDO) {
        return locationService.getLocationList(locationDO);
    }


}
